﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->comboBox->clear();
//    foreach (QSerialPortInfo info, QSerialPortInfo::availablePorts()) {
//        ui->comboBox->addItem(info.portName());
//    }

    ttu = new TTucmd(1,1,ui->lineEdit_3->text().trimmed(),ui->lineEdit_4->text().trimmed());

    connect(ttu,SIGNAL(destroyed()),this,SLOT(releaseThread()));
    connect(ttu,SIGNAL(RespondsetTTUIP(int ,bool)),this,SLOT(RespondsetTTUIP(int ,bool)));
    connect(ttu,SIGNAL(RespondgetVersion(int ,QString)),this,SLOT(RespondgetVersion(int ,QString)));
    connect(ttu,SIGNAL(RespondgetCPUInfo(int ,QMap<QString,QString>)),this,SLOT(RespondgetCPUInfo(int ,QMap<QString,QString>)));
    connect(ttu,SIGNAL(RespondcheckOnline(int ,bool)),this,SLOT(RespondcheckOnline(int ,bool)));
    connect(ttu,SIGNAL(RespondgetContainerSts(int ,QList<TTucmd::_ContainerInfo>)),this,SLOT(RespondgetContainerSts(int ,QList<TTucmd::_ContainerInfo>)));
    connect(ttu,SIGNAL(RespondstopContainer(int ,QString,bool)),this,SLOT(RespondstopContainer(int ,QString,bool)));
    connect(ttu,SIGNAL(RespondstartContainer(int ,QString,bool)),this,SLOT(RespondstartContainer(int ,QString,bool)));
    connect(ttu,SIGNAL(RespondgetAppInfo(int ,QList<TTucmd::_AppInfo>)),this,SLOT(RespondgetAppInfo(int ,QList<TTucmd::_AppInfo>)));
    connect(ttu,SIGNAL(RespondstopApp(int ,QString,QString,bool)),this,SLOT(RespondstopApp(int ,QString,QString,bool)));
    connect(ttu,SIGNAL(RespondstartApp(int ,QString,QString,bool)),this,SLOT(RespondstartApp(int ,QString,QString,bool)));
    connect(ttu,SIGNAL(RespondcopyApp(int ,QString,QString,bool)),this,SLOT(RespondcopyApp(int ,QString,QString,bool)));
    connect(ttu,SIGNAL(RespondinstallApp(int ,QString,QString,bool)),this,SLOT(RespondinstallApp(int ,QString,QString,bool)));
    connect(ttu,SIGNAL(ResponduninstallApp(int ,QString,QString,bool)),this,SLOT(ResponduninstallApp(int ,QString,QString,bool)));
    connect(ttu,SIGNAL(SendCmdMsg(int ,QString)),this,SLOT(SendCmdMsg(int ,QString)));
    connect(ttu,SIGNAL(RevCmdMsg(int ,QString)),this,SLOT(RevCmdMsg(int ,QString)));
    connect(ttu,SIGNAL(RespondNetMap(int,QString,bool)),this,SLOT(RespondNetMap(int,QString,bool)));
    connect(ttu,SIGNAL(RespondTime(bool)),this,SLOT(RespondTime(bool)));
    connect(ttu,SIGNAL(RespondPos(int,QString,QString,QString)),this,SLOT(RespondPos(int,QString,QString,QString)));
    connect(ttu,SIGNAL(ResondAppVersion(int,QString)),this,SLOT(respondAppVersion(int,QString)));
    connect(ttu,SIGNAL(SshConnected(int)),this,SLOT(respondSshConnted(int)));


    connect(this,SIGNAL(getSoftVersion()),ttu,SLOT(getVersion()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_Btn_setIP_clicked()
{
    ttu->setTTUIP(ui->lEdit_setIP->text().trimmed());
}

void MainWindow::on_Btn_software_clicked()
{
    emit getSoftVersion();
}

void MainWindow::on_Btn_Hardware_clicked()
{
    mOptFlags.cpuInfoFlag = 1;
    ttu->getCPUInfo();
}

void MainWindow::on_Btn_cpuInfo_clicked()
{
    mOptFlags.cpuInfoFlag = 2;
    ttu->getCPUInfo();
}

void MainWindow::on_Btn_stopcont_clicked()
{
    //mOptFlags.contInfoFlag = 1;
    QString str = ui->Btn_stopcont->text();
    if(str == g_C2Q("停止容器")){
        ttu->stopContainer(ui->lEdit_stopcont->text());
    }
    else{
        ttu->startContainer(ui->lEdit_stopcont->text());
    }
}

void MainWindow::on_Btn_stopapp_clicked()
{
    QString str = ui->Btn_stopapp->text();
    if(str == g_C2Q("停止app")){
        ttu->stopApp(ui->lEdit_stopcont->text(),ui->lEdit_stopapp->text());
    }
    else{
        ttu->startApp(ui->lEdit_stopcont->text(),ui->lEdit_stopapp->text());
    }
}

void MainWindow::on_Btn_appUsge_clicked()
{
    mOptFlags.appInfoFlag=1;
    ttu->getAppInfo(ui->lEdit_stopcont->text());
}

void MainWindow::on_Btn_cpuUsge_clicked()
{
    mOptFlags.contInfoFlag = 1;
    ttu->getContainerSts();
}

void MainWindow::on_Btn_connect_clicked()
{
    if(ui->Btn_connect->text() == g_C2Q("网口连接")){
        ui->Btn_connect->setText(g_C2Q("网口断开"));
        ttu->setSerialArgs(ui->lineEdit_ip->text(),ui->lineEdit_port->text().toInt());//ui->comboBox->currentText()

        ttu->openPort(0);

    }
    else{
        ui->Btn_connect->setText(g_C2Q("网口连接"));
        ttu->closePort();
    }
}

void MainWindow::RespondsetTTUIP(int devindex,bool ret)
{
    if(ret){
        ui->lEdit_setIP->setText(g_C2Q("设置成功!"));
    }
    else{
        ui->lEdit_setIP->setText(g_C2Q("设置失败!"));
    }
}

void MainWindow::RespondgetVersion(int devindex,QString ver)
{
    if(ver.isEmpty()){
        ui->lEdit_software->setText(g_C2Q("获取失败"));
    }
    else
        ui->lEdit_software->setText(ver);
}

void MainWindow::RespondgetCPUInfo(int devindex,QMap<QString, QString> cpuinfo)
{
    if(mOptFlags.cpuInfoFlag == 1){
        if(cpuinfo.contains("Hardware")){
            ui->lEdit_Hardware->setText(cpuinfo.value("Hardware"));
        }
        else{
            ui->lEdit_Hardware->setText("error!no value");
        }
    }
    else if(mOptFlags.cpuInfoFlag == 2){
        if(cpuinfo.contains("model name")){
            ui->lEdit_cpuInfo->setText(cpuinfo.value("model name"));
        }
        else{
            ui->lEdit_cpuInfo->setText("error!no value");
        }
    }
    else
        ui->lEdit_cpuInfo->setText("key error");

    mOptFlags.cpuInfoFlag = 0;
}

void MainWindow::RespondcheckOnline(int devindex,bool ret)
{
    if(ret){
        ui->lEdit_online->setText(g_C2Q("已上线"));
    }
    else{
        ui->lEdit_online->setText(g_C2Q("未上线"));
    }
}

void MainWindow::RespondgetContainerSts(int devindex,QList<TTucmd::_ContainerInfo> continfolist)
{
    if(continfolist.isEmpty()){
        qDebug() << "error: container info list is empty";
        return;
    }
    if(mOptFlags.contInfoFlag == 1){
        for(int i=0; i<continfolist.length(); ++i){
            TTucmd::_ContainerInfo info = continfolist.at(i);
            if(info.cpUsage!=-1){
                 ui->lEdit_cpuUsge->setText(QString("%1%").arg(info.cpUsage));
            }
            else{
                ui->lEdit_cpuUsge->setText("error!no value");
            }
        }
    }
    else if(mOptFlags.contInfoFlag == 2){
        for(int i=0; i<continfolist.length(); ++i){
            TTucmd::_ContainerInfo info = continfolist.at(i);
            if(!info.name.isEmpty()){
                 ui->textEdit->setText(QString("container name:%1").arg(info.name));
            }
            else{
                ui->textEdit->setText("error!no name");
            }
        }
    }
    else
        ui->lEdit_cpuUsge->setText("key error");

    mOptFlags.contInfoFlag = 0;
}

void MainWindow::RespondstopContainer(int devindex,QString name, bool ret)
{
    QString str = ui->Btn_stopcont->text();
    if(str == g_C2Q("停止容器")){
        if(ret){
            ui->lEdit_stopcont->setText(g_C2Q("%1停止成功").arg(name));
            ui->Btn_stopcont->setText(g_C2Q("启动容器"));
        }
        else{
            ui->lEdit_stopcont->setText(g_C2Q("%1停止失败").arg(name));
        }
    }
}

void MainWindow::RespondstartContainer(int devindex,QString name, bool ret)
{
    QString str = ui->Btn_stopcont->text();
    if(str == g_C2Q("启动容器")){
        if(ret){
            ui->lEdit_stopcont->setText(g_C2Q("%1启动成功").arg(name));
            ui->Btn_stopcont->setText(g_C2Q("停止容器"));
        }
        else{
            ui->lEdit_stopcont->setText(g_C2Q("%1启动失败").arg(name));
        }
    }
}

void MainWindow::RespondgetAppInfo(int devindex,QList<TTucmd::_AppInfo> appinfo)
{
    int index = -1;
    for(int i=0; i<appinfo.length(); ++i){
        qDebug() << appinfo.at(i).serviceName;
        if(appinfo.at(i).serviceName == ui->lEdit_stopapp->text()){
            index = i;
        }
    }

    if(index==-1){
        qDebug() << "can't find app " <<ui->lEdit_stopapp->text();
        return;
    }

    if(mOptFlags.appInfoFlag == 1){
        ui->lEdit_appUsge->setText(QString("%1%").arg(appinfo.at(index).cpuThreshold));
    }
    else if(mOptFlags.appInfoFlag == 2){
        ui->lEdit_appUsge->setText(appinfo.at(index).servicStatus);
    }
    else
        ui->lEdit_appUsge->setText("key error");

    mOptFlags.appInfoFlag = 0;
}

void MainWindow::RespondstopApp(int devindex,QString contname, QString appname, bool ret)
{
    QString str = ui->Btn_stopapp->text();
    if(str == g_C2Q("停止app")){
        if(ret){
            ui->lEdit_stopapp->setText(g_C2Q("容器%1停止app %2成功").arg(contname).arg(appname));
            ui->Btn_stopapp->setText(g_C2Q("启动app"));
        }
        else{
            ui->lEdit_stopapp->setText(g_C2Q("容器%1停止app %2失败").arg(contname).arg(appname));
        }
    }
}

void MainWindow::RespondstartApp(int devindex,QString contname, QString appname, bool ret)
{
    QString str = ui->Btn_stopapp->text();
    if(str == g_C2Q("停止app")){
        if(ret){
            ui->lEdit_stopapp->setText(QString("容器%1停止app 成功").arg(contname).arg(appname));
            ui->Btn_stopapp->setText(g_C2Q("停止app"));
        }
        else{
            ui->lEdit_stopapp->setText(QString("容器%1停止app 失败").arg(contname).arg(appname));
        }
    }
}

void MainWindow::RespondcopyApp(int devindex,QString path, QString,bool)
{

}

void MainWindow::RespondinstallApp(int devindex,QString, QString, bool)
{

}

void MainWindow::ResponduninstallApp(int devindex,QString, QString, bool)
{

}

void MainWindow::SendCmdMsg(int devindex,QString msg)
{
    ui->textEdit->append("send--->>> "+msg);
}

void MainWindow::RevCmdMsg(int devindex,QString msg)
{
    ui->textEdit->append("receive<<<--- "+msg);
}

void MainWindow::RespondNetMap(int,QString info, bool ret)
{
    if(ret){
        qDebug() << "add net map success!";
    }
    else{
        qDebug() << "add net map fail!" << info;
    }
}

void MainWindow::RespondTime(bool ret)
{
    ui->textEdit->append(g_C2Q("对时结果：%1").arg(ret));
}

void MainWindow::RespondPos(int index,QString e, QString n, QString h)
{
    ui->textEdit->append(g_C2Q("位置：e:%1,n:%2,h:%3").arg(e).arg(n).arg(h));
}

void MainWindow::respondAppVersion(int index, QString appVersion)
{
    ui->textEdit->append(g_C2Q("AppVersion：%1").arg(appVersion));
}

void MainWindow::respondSshConnted(int index)
{
    qDebug()<<(g_C2Q("%1号设备已连接").arg(index));
}

void MainWindow::on_Btn_online_clicked()
{
    ttu->checkOnline();
}

void MainWindow::on_Btn_netmap_clicked()
{
    ttu->setIp(ui->lEdit_netmap->text().trimmed());
    ttu->setStep();
    ttu->setNetMap();
    ttu->mapInterAcess();
}

void MainWindow::on_pushButton_clicked()
{
    TTucmd *ttuc = new TTucmd(1,1,"sysadm","Zxbdt@729.TTU");
    connect(ttuc,SIGNAL(RespondsetTTUIP(int ,bool)),this,SLOT(RespondsetTTUIP(int ,bool)));
    connect(ttuc,SIGNAL(RespondgetVersion(int ,QString)),this,SLOT(RespondgetVersion(int ,QString)));
    connect(ttuc,SIGNAL(RespondgetCPUInfo(int ,QMap<QString,QString>)),this,SLOT(RespondgetCPUInfo(int ,QMap<QString,QString>)));
    connect(ttuc,SIGNAL(RespondcheckOnline(int ,bool)),this,SLOT(RespondcheckOnline(int ,bool)));
    connect(ttuc,SIGNAL(RespondgetContainerSts(int ,QList<TTucmd::_ContainerInfo>)),this,SLOT(RespondgetContainerSts(int ,QList<TTucmd::_ContainerInfo>)));
    connect(ttuc,SIGNAL(RespondstopContainer(int ,QString,bool)),this,SLOT(RespondstopContainer(int ,QString,bool)));
    connect(ttuc,SIGNAL(RespondstartContainer(int ,QString,bool)),this,SLOT(RespondstartContainer(int ,QString,bool)));
    connect(ttuc,SIGNAL(RespondgetAppInfo(int ,QList<TTucmd::_AppInfo>)),this,SLOT(RespondgetAppInfo(int ,QList<TTucmd::_AppInfo>)));
    connect(ttuc,SIGNAL(RespondstopApp(int ,QString,QString,bool)),this,SLOT(RespondstopApp(int ,QString,QString,bool)));
    connect(ttuc,SIGNAL(RespondstartApp(int ,QString,QString,bool)),this,SLOT(RespondstartApp(int ,QString,QString,bool)));
    connect(ttuc,SIGNAL(RespondcopyApp(int ,QString,QString,bool)),this,SLOT(RespondcopyApp(int ,QString,QString,bool)));
    connect(ttuc,SIGNAL(RespondinstallApp(int ,QString,QString,bool)),this,SLOT(RespondinstallApp(int ,QString,QString,bool)));
    connect(ttuc,SIGNAL(ResponduninstallApp(int ,QString,QString,bool)),this,SLOT(ResponduninstallApp(int ,QString,QString,bool)));
    connect(ttuc,SIGNAL(SendCmdMsg(int ,QString)),this,SLOT(SendCmdMsg(int ,QString)));
    connect(ttuc,SIGNAL(RevCmdMsg(int ,QString)),this,SLOT(RevCmdMsg(int ,QString)));
    connect(ttuc,SIGNAL(RespondTime(bool)),this,SLOT(RespondTime(bool)));
    connect(ttuc,SIGNAL(RespondPos(double,double,double)),this,SLOT(RespondPos(double,double,double)));
    connect(ttuc,SIGNAL(ResondAppVersion(int,QString)),this,SLOT(respondAppVersion(int,QString)));

    static int i= 1;
    //ttuc->setSerialArgs(QString("COM%1").arg(i++));
    //ttuc->openPort();
}

void MainWindow::on_pushButton_Send_clicked()
{
    ttu->sendData(ui->lineEdit->text());
}

void MainWindow::on_pushButton_2_clicked()
{
    ttu->BdTime();
}

void MainWindow::on_pushButton_3_clicked()
{
    ttu->BdPos();
}

void MainWindow::on_pushButton_4_clicked()
{
    if(ui->pushButton_4->text() == g_C2Q("串口连接")){
        ui->pushButton_4->setText(g_C2Q("串口断开"));
        ttu->setSerialArgs(ui->comboBox->currentText(),ui->lineEdit_2->text().toInt(),0);//ui->comboBox->currentText()

        ttu->openPort(1);
    }
    else{
        ui->pushButton_4->setText(g_C2Q("串口连接"));
        ttu->closePort(1);
    }
}

void MainWindow::on_pushButton_5_clicked()
{
    ttu->queryProcess();
}

void MainWindow::on_pushButton_6_clicked()
{
    ttu->SetAuthority();
}

void MainWindow::on_pushButton_7_clicked()
{
    ttu->StartExeProgram();
}

void MainWindow::on_pushButton_8_clicked()
{

    ttu->SetCombinationKeys();
}

void MainWindow::on_pushButton_9_clicked()
{
    ttu->RestartContainer();
}

void MainWindow::on_pushButton_10_clicked()
{
    ttu->getAppVersion("lxc01","customparm");
}

void MainWindow::releaseThread()
{
    pthread->quit();
    pthread->wait();
}
